package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.hospital;

import com.bmps.springcloud.insuredmicroservice.config.accept.ProductConfig;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.QuotaConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.AcceptRequestDataModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.InsurantModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * description:
 * 累积风险保额 险种HMT008
 *
 * @author jackdaw
 * @date 2019-10-28 9:39
 */
public class Hmt008AccumulatedStrategy implements HospitalStrategy {

    @Override
    public InsuredModel checkHospitalRolesByRiskCode(AcceptRequestDataModel acceptRequestDataModel, InsuredCoreRepository insuredCoreRepository, ProductModel productModel, RedisService redisService, ProductConfig productConfig) {
        RequestModel requestModel = acceptRequestDataModel.getRequest();
        List<InsurantModel> insurantModelList = requestModel.getCustInfo().getInsurants().getInsurant();
        int prodInsSeq = productModel.getProdInsSeq();
        //HMT008保额与主险保额比例不能超过1:5
        if (!this.checkProportion(productModel.getAmnt(), this.getMainRisk(requestModel, redisService))) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_NUGG0037_HMT008_MESSAGE);
        }
        //累积住院险保额计算
        for (InsurantModel insurantModel : insurantModelList) {
            if (insurantModel.getInsSeq() == prodInsSeq) {
                //计算当前单住院险保额
                BigDecimal nowHosp = this.getNowHospInsuAmnt(prodInsSeq, acceptRequestDataModel.getRequest().getProductInfo().getProducts().getProduct());
                BigDecimal cacheTotal = redisService.getCacheQuota(QuotaConstant.SORT_C, insurantModel.getIdNo(), nowHosp, requestModel.getPolicyInfo().getContNo());
                //查询历史单住院险保额
                double historyHosp = insuredCoreRepository.getHistoryHospInsuAmnt(requestModel.getPolicyInfo().getPrtNo(), insurantModel.getInsuredNo());
                BigDecimal total = nowHosp.add(BigDecimal.valueOf(historyHosp)).add(cacheTotal);
                //校验被保人累积住院险保额是是否超过1万
                if (AcceptUtil.compareTo(total, BigDecimal.valueOf(10000)) == 1) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                            String.format(UnderWriteConstant.UNDER_WRITE_NUGG0038_HMT008_MESSAGE, insurantModel.getName()));
                }
            }
        }
        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE, AcceptConstant.RESULT_SUCCESS_MESSAGE);
    }

    private boolean checkProportion(BigDecimal amnt, ProductModel mainProductModel) {
        return (amnt.doubleValue() / mainProductModel.getAmnt().doubleValue() < AcceptConstant.ZERO_POINT_TWO);
    }

    private ProductModel getMainRisk(RequestModel requestModel, RedisService redisService) {
        List<ProductModel> productModels = requestModel.getProductInfo().getProducts().getProduct();
        for (ProductModel productModel : productModels) {
            if (redisService.isMainRisk(productModel.getProdCode())) {
                return productModel;
            }
        }
        return new ProductModel();
    }

    private BigDecimal getNowHospInsuAmnt(int insSeq, List<ProductModel> productModelList) {
        BigDecimal total = BigDecimal.valueOf(0);
        for (ProductModel productModel : productModelList) {
            if (productModel.getProdInsSeq() == insSeq) {
                if ((StringUtils.equals("HMT008", productModel.getProdCode()) ||
                        StringUtils.equals("HMT009", productModel.getProdCode()))) {
                    total = total.add(productModel.getAmnt());
                }
            }
        }
        return total;
    }
}
